package com.sen.gulimall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sen.common.utils.PageUtils;
import com.sen.common.utils.Query;
import com.sen.gulimall.coupon.dao.SeckillSessionDao;
import com.sen.gulimall.coupon.entity.SeckillSessionEntity;
import com.sen.gulimall.coupon.entity.SeckillSkuRelationEntity;
import com.sen.gulimall.coupon.service.SeckillSessionService;
import com.sen.gulimall.coupon.service.SeckillSkuRelationService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {


    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getLatest3DaySession() {
        //计算最近三天
        //查出这三天参与秒杀活动的商品
        QueryWrapper<SeckillSessionEntity> queryWraper = new QueryWrapper<>();
        queryWraper.between("start_time", startTime(), endTime());
        List<SeckillSessionEntity> list = this.baseMapper.selectList(queryWraper);

        if (CollectionUtils.isNotEmpty(list)) {
            List<SeckillSessionEntity> collect = list.stream().map(session -> {
                Long id = session.getId();
                List<SeckillSkuRelationEntity> relationSkus = seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>()
                        .eq("promotion_session_id", id));
                session.setRelationSkus(relationSkus);

                return session;
            }).collect(Collectors.toList());
            return collect;
        }

        return Collections.emptyList();
    }

    private String startTime() {
        LocalDate now = LocalDate.now();
        LocalDateTime start = LocalDateTime.of(now, LocalTime.MIN);
        String format = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }

    private String endTime() {
        LocalDate plus = LocalDate.now().plusDays(2);
        LocalDateTime end = LocalDateTime.of(plus, LocalTime.MAX);
        String format = end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }

    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        LocalDate plus = now.plusDays(2);
        System.out.println(plus);

        LocalTime min = LocalTime.MIN;
        LocalTime max = LocalTime.MAX;
        LocalDateTime start = LocalDateTime.of(now, min);
        LocalDateTime end = LocalDateTime.of(plus, max);

        String startFormat = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String endFormat = end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println(startFormat);
        System.out.println(endFormat);


    }

}